package cn.smart99.homepage.ui.view;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.smart99.homepage.R;

/**
 * TODO: document your custom view class.
 */
public class TitleView extends RelativeLayout {
    private static final  String TAG = "TitleView";

    private Context context;
    private View view;
    private ImageView imgNetworkState;
    private TextView  tvTime;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
        if(!isInEditMode())
            init();
    }


    private void init() {
        view = LayoutInflater.from(context).inflate(R.layout.title_view, this);
        imgNetworkState = (ImageView)view.findViewById(R.id.imageview_networkstate);
        tvTime = (TextView)view.findViewById(R.id.textview_time);

    }

    public void registerReceiver() {
        Log.d(TAG,"registerReceiver");
        setTvTime(getTime());
        context.registerReceiver(mConnReceiver, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));
        context.registerReceiver(wifiChange, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
        context.registerReceiver(timeReceiver, new IntentFilter(Intent.ACTION_TIME_TICK));
        context.registerReceiver(timeReceiver, new IntentFilter(Intent.ACTION_TIME_CHANGED));
    }

    public void unregisterReceiver() {
        Log.d(TAG,"unregisterReceiver");
        context.unregisterReceiver(mConnReceiver);
        context.unregisterReceiver(wifiChange);
        context.unregisterReceiver(timeReceiver);
    }

    public void setTvTime(String  time){
        tvTime.setText(time);
    }

    public void setImgNetworkState(int state){
        imgNetworkState.setImageResource(state);
    }

    /**
    *  时间,网络监测
    * */

    private BroadcastReceiver timeReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            setTvTime(getTime());
        }
    };

    private BroadcastReceiver wifiChange = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            WifiManager wifiManager = (WifiManager)context
                    .getSystemService(Context.WIFI_SERVICE);
            WifiInfo wifiInfo = wifiManager.getConnectionInfo();
            if(wifiInfo.getBSSID() != null){
                int signalLevel = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), 5);
                if(signalLevel == 0){
                    setImgNetworkState(R.drawable.wifi_0);
                }
                else if(signalLevel == 1){
                    setImgNetworkState(R.drawable.wifi_1);
                }
                else if(signalLevel == 2){
                    setImgNetworkState(R.drawable.wifi_2);
                }
                else if(signalLevel == 3){
                    setImgNetworkState(R.drawable.wifi_3);
                }
                else if(signalLevel == 4){
                    setImgNetworkState(R.drawable.wifi_4);
                }
                Log.d(TAG, "wifi level = " + signalLevel);
            }



        }
    };

    private  BroadcastReceiver mConnReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if(networkInfo.isConnected()){
                if(networkInfo.getType() == ConnectivityManager.TYPE_WIFI){
                    setImgNetworkState(R.drawable.wifi_0);
                }
                else if(networkInfo.getType() == ConnectivityManager.TYPE_ETHERNET){
                    setImgNetworkState(R.drawable.wired);
                }

            }else {
                setImgNetworkState(R.drawable.wifi_error);
            }
           Log.d(TAG, "isconnected = " + networkInfo.isConnected());
           Log.d(TAG, "type = " + networkInfo.getType());
        }
    };

    /**
     *
     * 工具函数
     * 获取时间
     *
     */
    private static String getTime() {
        String date = getFormattedDate();
        String sTime = date.substring(11, date.length() - 3);
        Log.d(TAG,"date = " + date);
        return sTime;

    }

    private static String getDate() {
        String date = getFormattedDate();
        String sDate = date.substring(0, 11);
        return sDate;
    }

    private static String getFormattedDate() {

        DateFormat.getDateInstance();
        Calendar c = Calendar.getInstance();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String formattedDate = df.format(c.getTime());
        return formattedDate;
    }



}
